package admin.subject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.Const;
import com.common.Dateutil;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.subject.subchapterBean;
import beans.subject.ChapterModelSearch;
import beans.sys.SYSCodeBean;
import beans.sys.UserBean;
import service.subject.SubChapterServiceImpl;
import service.sys.SYSCodeServiceImpl;

@Controller
@RequestMapping("/subject")
public class ChapterController  extends BaseController{

	@Resource
	private SubChapterServiceImpl<subchapterBean> _SubChapterServiceImpl;
	
	@Resource
	private SYSCodeServiceImpl<SYSCodeBean> _SYSCodeServiceImpl;
	
	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/index")
	public String Index() {
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");
		getReq().setAttribute("CarTypeList", list);
		
		return "admin/chapter/index";
	}
	
	@RequestMapping("/GetData")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="subject")
	public void DataGrid(ChapterModelSearch model){
		ResultData<List<subchapterBean>> rd = new ResultData<>();
		try{
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");

			rd.setPage(model.getPage());

			if(!CommonUtil.isNullObject(model.getEndtime())){
				model.setEndtime(Dateutil.nextDate(model.getEndtime()));
			}

			List<subchapterBean> m = _SubChapterServiceImpl.queryByList(model);
			
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/detail")
	public String Detail(String id) {
		subchapterBean m = null;
		if(!CommonUtil.isNullObject(id)){
			try {
				m = _SubChapterServiceImpl.queryById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		List<Map> list = _SYSCodeServiceImpl.getListByCode("CarType");

		getReq().setAttribute("CarTypeList", list);
		getReq().setAttribute("chapter", m);
		return "admin/chapter/detail";
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/save")
	@ResponseBody
	public void Save(subchapterBean m) {
		ResultData<String> rd = new ResultData<>();
		try {
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			if(CommonUtil.isNullObject(m.getId())) {
				m.setId(UUID.randomUUID().toString());
				m.setAddtime(new Date());
				m.setAdduser(u.getId());
				m.setSubcount(0);
				_SubChapterServiceImpl.add(m);
			}
			else {
				m.setEdittime(new Date());
				m.setEdituser(u.getId());
				_SubChapterServiceImpl.update(m);
			}
		} catch (Exception e) {
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();
			return;
		}
		rd.setType(1);
		rd.setMessage("保存成功!");
		responseJson(rd);
	}

	@AuthPassport(actionName="index",controllerName="subject")
	@RequestMapping("/delete")
	@ResponseBody
	public void Delete() {
		ResultData<String> rd = new ResultData<>();
		String[] ids = getReq().getParameterValues("ids");
		try{
			List<subchapterBean> list = _SubChapterServiceImpl.queryByIds(ids);
			for(subchapterBean chapter : list) {
				if(chapter.getSubcount()!=0) {
					rd.setType(3);
					rd.setMessage(chapter.getName()+"章节下有题目,请确认!");
					responseJson(rd);
					return;
				}
			}
			_SubChapterServiceImpl.deletes(ids);
			rd.setType(1);
			rd.setMessage("删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
			rd.setType(3);
			rd.setMessage("删除失败");
		}
		responseJson(rd);
	}
}
